import React from 'react';
import {Button, Icon, message, Modal} from 'antd';
import {WeaTools, WeaLocaleProvider, WeaNewScroll, WeaTree} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

class MaterialLibLeft extends React.Component {
    state = {datas: []};

    constructor(props) {
        super(props);
        this.onSelect = this.onSelect.bind(this);
        this.getDirs = this.getDirs.bind(this);
        this.onLoadData = this.onLoadData.bind(this);
        this.onAdd = this.onAdd.bind(this);
        this.onEdit = this.onEdit.bind(this);
        this.onDelete = this.onDelete.bind(this);
        this.onValidate = this.onValidate.bind(this);
        this.onOpsCheck = this.onOpsCheck.bind(this);
    }

    componentWillMount() {
        this.getDirs();
    }

    render() {
        const {hasRight} = this.props;
        const {datas} = this.state;

        return (
            <div className="portal-p4e-mll">
                <div className="portal-p4e-mll-top">
                    <Icon type="bars"/>
                    <span style={{marginLeft: '5px'}}>{getLabel(385079, '全部目录')}</span>
                </div>
                <div className="portal-p4e-mll-middle">
                    <WeaNewScroll height="100%">
                        <WeaTree
                            datas={datas}
                            loadData={this.onLoadData}
                            onSelect={this.onSelect}
                            onAdd={this.onAdd}
                            onEdit={this.onEdit}
                            onDelete={this.onDelete}
                            validate={this.onValidate}
                            onOpsCheck={this.onOpsCheck}
                        />
                    </WeaNewScroll>
                </div>
                {
                    hasRight ? (
                            <div className="portal-p4e-mll-bottom">
                                <Icon type="exclamation-circle-o" style={{"margin-left": "-150px"}}/>
                                <span style={{marginLeft: '5px' ,"width": "175px",
                                    "cursor": "pointer",
                                    "position": "absolute",
                                    "text-overflow": "ellipsis",
                                    "white-space": "nowrap",
                                    "overflow": "hidden"}} title={getLabel(387215, '目录名称只能包含字母和数字')} >{getLabel(387215, '目录名称只能包含字母和数字')}</span>
                            </div>
                        ) : ''
                }
            </div>
        );
    }

    onSelect(dirs) {
        this.props.setDir(dirs[0]);
    }

    getDirs() {
        WeaTools.callApi('/api/portal/materialLib/getDirs', 'GET', {}).then(result => {
            this.setState({datas: result.datas || []});
        });
    }

    onLoadData(node) {
        return WeaTools.callApi('/api/portal/materialLib/getDirs', 'GET', {dir: node.dir});
    }

    onOpsCheck(type, item) {  //删除、编辑校验
        const { isParent } = item;
        const {isDefaultFile } = item;
        if(type == 'add' || !isDefaultFile) return true;
        if(type == 'edit'){
            Modal.error({
                title: getLabel(15172, '系统提示'),
                content: getLabel(389194, '系统默认目录不支持编辑！')
            });
            return false;
        }else if(type == 'delete'){
            Modal.error({
                title: getLabel(15172, '系统提示'),
                content: getLabel(389195, '系统默认目录不支持删除！')
            });
            return false;
        }
    }

    onAdd(value, node) {
        return WeaTools.callApi('/api/portal/materialLib/addDir', 'POST', {dir: node.dir, dirname: value});
    }

    onEdit(value, node) {
        return WeaTools.callApi('/api/portal/materialLib/renameDir', 'POST', {pdir: node.parentId, dir: node.dir, dirname: value});
    }

    onDelete(node) {
        WeaTools.callApi('/api/portal/materialLib/deleteDir', 'POST', {dir: node.dir});
    }

    onValidate(node) {
        if (!node.name.toLowerCase().match(/^[0-9A-Za-z]+$/)) {
            message.warning(getLabel(387215, '目录名称只能包含字母和数字'));
            return false;
        } else {
            return true;
        }
    }
}

export default MaterialLibLeft;
